//
//  LYYForumParkView.m
//  Icar
//
//  Created by dllo on 15/10/19.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "LYYForumParkView.h"
#import "LYYHeaderCollectionReusableView.h"
#import "LYYHotColumnCollectionViewCell.h"
#import "LYYGuessLikeCollectionViewCell.h"
#import "LYYGuessSecondCollectionViewCell.h"
#import "BaseHeader.h"
#import "LYYPublishViewController.h"
#import "LYYWebviewViewController.h"
@interface LYYForumParkView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    NSInteger sectionCount;
}

@property (nonatomic,retain)DENForumModel *model;
@property (nonatomic,assign)BOOL isUp;
@property (nonatomic,assign)NSInteger page;
@end

@implementation LYYForumParkView


- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = 5;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.headerReferenceSize = CGSizeMake(WIDTH(375), 40);
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH(370), HEIGHT(667)) collectionViewLayout:flowLayout];
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        [self addSubview:self.collectionView];
        [self.collectionView registerClass:[LYYGuessLikeCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
        [self.collectionView registerClass:[LYYGuessSecondCollectionViewCell class] forCellWithReuseIdentifier:@"reuseSecond"];
        [self.collectionView registerClass:[LYYHotColumnCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self.collectionView registerClass:[LYYHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
        self.hotMutableArray = [[NSMutableArray alloc]init];
        self.guessLikeArray = [[NSMutableArray alloc]init];
        [self headerRefresh];
        [self footerRefresh];
        [self getHotDataSource];
        self.collectionView.showsVerticalScrollIndicator = NO;
        [self.collectionView reloadData];
        
    }
    return self;
}

#pragma mark - 设置sectioninsets
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    if (section == 0) {
        return UIEdgeInsetsMake(5, 20, 5, 20);
    }else{
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
    
}

#pragma mark - item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return CGSizeMake(WIDTH(100), HEIGHT(40));
    }else{
        DENForumModel *nemo = [[DENForumModel alloc]init];
        nemo = [self.guessLikeArray objectAtIndex:indexPath.row];
        if (nemo.topicimg.count != 0) {
            return CGSizeMake(WIDTH(375), HEIGHT(190));
        }else{
            return CGSizeMake(WIDTH(375), HEIGHT(120));
        }
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section == 0) {
        return self.hotMutableArray.count;
    }else{
        return self.guessLikeArray.count;
    }
}
#pragma mark - cell 赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    sectionCount = indexPath.section;
    if (indexPath.section == 0) {
        LYYHotColumnCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        LYYHotColumnModel *model = [self.hotMutableArray objectAtIndex:indexPath.row];
        [cell getModel:model];
        cell.forumNameLabel.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor;
        cell.forumNameLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
        return cell;

    }else{
        self.model = [self.guessLikeArray objectAtIndex:indexPath.row];
        if (self.model.topicimg.count != 0) {
            LYYGuessLikeCollectionViewCell *reuse = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
            reuse.titleLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
            [reuse getInfoModel:self.model];
            [reuse setBackgroundColor:[LYYNightModeHandle shareNightModeHandle].nightModeColor];
            return reuse;
        }else if(self.model.topicimg.count == 0){
            LYYGuessSecondCollectionViewCell *reuseSecond = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseSecond" forIndexPath:indexPath];
            [reuseSecond getSecondModel:self.model];
            reuseSecond.titleLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
            [reuseSecond setBackgroundColor:[LYYNightModeHandle shareNightModeHandle].nightModeColor];
            return reuseSecond;
        }
    }
    return nil;
}

#pragma mark - headerSection
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *reusableView = nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        LYYHeaderCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            [header.imView setImage:[UIImage imageNamed:@"bbs_hot_channel"]];
            [header.label setText:@"热门板块"];
        }else{
            [header.imView setImage:[UIImage imageNamed:@"bbs_guess_like"]];
            [header.label setText:@"猜你喜欢"];
        }
        reusableView = header;
    }
    return reusableView;
}

//热门数据解析
- (void)getHotDataSource{
    NSString *cacheString = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *pathString = [cacheString stringByAppendingString:@"/hot.xx"];
    NSLog(@"pathString = %@",pathString);
    if (ISCONENNCTED == YES) {
        [AFNBlock getDataWithString:@"http://mrobot.pcauto.com.cn/v3/bbs/hotForums?" block:^(id back) {
            
            for (NSDictionary *dic in back) {
                self.hotModel = [LYYHotColumnModel modelWithDictionary:dic];
                [self.hotMutableArray addObject:self.hotModel];
            }
            [self.collectionView reloadData];
            
           //编码
            [NSKeyedArchiver archiveRootObject:self.hotMutableArray toFile:pathString];
        }];
    }
    if (ISCONENNCTED == NO) {
        self.hotModel = [[LYYHotColumnModel alloc]init];
        self.hotMutableArray = [NSKeyedUnarchiver unarchiveObjectWithFile:pathString];
        [self.collectionView reloadData];
    }
    
}
//猜你喜欢数据解析
- (void)getGuessLikeDateSource: (NSInteger) number{
    NSString *cacheString = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *pathString = [cacheString stringByAppendingString:@"/guess.xx"];
    if (ISCONENNCTED == YES) {
        [AFNBlock getDataWithString:@"http://mrobot.pcauto.com.cn/v3/bbs/guessYouLike?visitedFid=19635D16300D&picRule=1" block:^(id back) {
            if (_isUp == YES) {
                [self.guessLikeArray removeAllObjects];
            }
            for (NSDictionary *guessDic in back) {
                self.guessModel = [DENForumModel modelWithDictionary:guessDic];
                [self.guessLikeArray addObject:self.guessModel];
            }
            //编码
            [NSKeyedArchiver archiveRootObject:self.guessLikeArray toFile:pathString];
            [self.collectionView reloadData];
            [self.collectionView.header endRefreshing];
            [self.collectionView.footer endRefreshing];
        }];
    }
    if (ISCONENNCTED == NO) {
        
        self.guessModel = [[DENForumModel alloc]init];
        self.guessLikeArray = [NSKeyedUnarchiver unarchiveObjectWithFile:pathString];
        [self.collectionView reloadData];
        
    }
    
}
#pragma mark - 下拉加载
- (void)headerRefresh{
    __block LYYForumParkView *view = self;
    [self.collectionView addLegendHeaderWithRefreshingBlock:^{
        view.page = 1;
        [view getGuessLikeDateSource:1];
        _isUp = YES;
    }];
    [self.collectionView.header beginRefreshing];
}
#pragma mark - 上拉刷新
- (void)footerRefresh{
    
    __block LYYForumParkView *view = self;
    [self.collectionView addLegendFooterWithRefreshingBlock:^{
        view.page = view.page + 1;
        [view getGuessLikeDateSource:view.page];
        _isUp = NO;
        
    }];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        LYYPublishViewController *publish = [[LYYPublishViewController alloc]init];
        [self.rootVC.navigationController pushViewController:publish animated:YES];
        LYYHotColumnModel *momo = [self.hotMutableArray objectAtIndex:indexPath.row];
        publish.model = momo;
        
    }else{
        DENForumModel *model = [self.guessLikeArray objectAtIndex:indexPath.row];
        LYYWebviewViewController *webview = [[LYYWebviewViewController alloc]init];
        [self.rootVC.navigationController pushViewController:webview animated:YES];
        webview.model = model;
    }
    
}

@end
